#include "Building.h"
#include "config.h"
#include "GameMap.h"
Building::Building()
{
    shape.name = NONE;
    state = TickableStatus::EMPTY;
    running_ms = 0;
    timer.Reset();
}

Building::Building(GridVec _pos, int _name, int _direction)
{
    pos = _pos;
    name = _name;
    direction = _direction;
    shape.name = NONE;
    state = TickableStatus::EMPTY;
    running_ms = 0;
    timer.Reset();
}

bool Building::operator==(const Building &b) const
{
    return (pos.i == b.pos.i && pos.j == b.pos.j);
}

bool Building::operator!=(const Building &b) const
{
    return !(this == &b);
}

bool Building::IsPlace(const GridVec &click, int picdirection, GameMap &gamemap)
{
    // miner重载
    // 如果超出地图范围，返回false
    if (click.i < 0 || click.i >= GameInfo::HEIGHT || click.j < 0 || click.j >= GameInfo::WIDTH)
    {
        return false;
    }
    // 如果在矿地上，或有障碍物，返回false
    if (gamemap.GetResource(click))
    {
        return false;
    }
    // 如果有其他建筑，返回false
    std::vector<GridVec> allpos = BuildingAllPos();
    for (auto pos : allpos)
    {
        if (gamemap.GetBuilding(pos))
        {
            return false;
        }
    }
    return true;
}
bool Building::CanSend(GridVec source, int directionout, int shapename, GameMap &map)
{
    // TODO
    if (name == BuildingType::HUB || name == BuildingType::TRASH)
    {
        return false;
    }
    GridVec target = map.GetTatget(source, directionout);
    if (target.i < 0 || target.i >= GameInfo::HEIGHT || target.j < 0 || target.j >= GameInfo::WIDTH)
    {
        return false;
    }
    if (map.GetBuilding(target) == nullptr)
    {
        return false;
    }
    if (map.GetBuilding(target)->CanReceive(target, directionout, shapename))
    {
        return true;
    }
    return false;
}

void Building::Send(GridVec source, int directionout, int shapename, GameMap &map)
{
    // TODO
    GridVec target = map.GetTatget(source, directionout);
    map.GetBuilding(target)->Receive(target, directionout, shapename);
    shape.name = NONE;
}
